package com.wanxi.dao.impl;//package com.wanxi.dao.impl;
//
//import com.wanxi.dao.NewsDao;
//import com.wanxi.model.NewsModel;
//import com.wanxi.model.ProductModel;
//import com.wanxi.util.JDBC;
//
//import java.math.BigDecimal;
//import java.sql.ResultSet;
//import java.sql.SQLException;
//import java.util.ArrayList;
//import java.util.List;
//
//public class NewsImpl implements NewsDao {
//    @Override
//    public int enable(NewsModel newsModel) {
//        String sql="update news set enable =case enable when 0 then 1 when 1 then 0 when -1 then 1 end where id="+newsModel.getId()+"";
////        String sql="update product set enable=1-enable where id="+productModel.getId()+"";
////        String sql = " update product set enable=enable^1 where id=  '"+ productModel.getId() + "'";
//
//        return JDBC.update(sql);
//    }
//
//    @Override
//    public int isShow(NewsModel newsModel) {
//        String sql="update news set is_show =case is_show when 0 then 1 when 1 then 0 when -1 then 1 end where id="+newsModel.getId()+"";
////        String sql="update product set enable=1-enable where id="+productModel.getId()+"";
////        String sql = " update product set is_show=is_show^1 where id=  '"+ productModel.getId() + "'";
//        int rows = JDBC.update(sql);
//        return rows;
//    }
//
//    @Override
//    public int isRecommend(NewsModel newsModel) {
//        String sql="update news set is_recommend =case is_recommend when 0 then 1 when 1 then 0 when -1 then 1 end where id="+newsModel.getId()+"";
////        String sql = " update product set is_recommend=is_recommend^1 where id=  '"+ productModel.getId() + "'";
//
//        return JDBC.update(sql);
//    }
//
//    @Override
//    public int whereShow(NewsModel newsModel) {
//        return 0;
//    }
//
//    @Override
//    public int add(NewsModel newsModel) {
//        String sql="insert into news (`name`,`source`,`author`,`number`,`subject`,`content`,`enable`,`is_show`,`is_recommend`,`where_show`,`create_time`,`newsclassification_id`) " +
//                "value('"+newsModel.getName()+"','"+newsModel.getSource()+"','"+newsModel.getAuthor()+"','"+newsModel.getNumber()+"','"+newsModel.getSubject()+"','"+newsModel.getContent()+"','"+newsModel.getEnable()+"','"+newsModel.getIsShow()+"','"+newsModel.getIsRecommend()+"','"+newsModel.getWhereShow()+"','"+newsModel.getCreateTime()+"','"+newsModel.getNewsclassificationId()+"')";
//        return JDBC.update(sql);
//    }
//
//    @Override
//    public int update(NewsModel newsModel) {
//        String sql = "update news set ";
//        if (!"".equals(newsModel.getName())) {
//            sql += " name ='" + newsModel.getName() + "',";
//
//        }
//        if (newsModel.getNewsclassificationId()!=null&&newsModel.getNewsclassificationId()!= -1) {
//            sql += " newsclassification_id ='" + newsModel.getNewsclassificationId() + "',";
//
//        }
//        if (!"".equals(newsModel.getSource())) {
//            sql += " source ='" + newsModel.getSource() + "',";
//
//        }
//        if (!"".equals(newsModel.getAuthor())) {
//            sql += " author ='" + newsModel.getAuthor() + "',";
//
//        }
//        if (!"".equals(newsModel.getCreateTime())) {
//            sql += " create_time ='" + newsModel.getCreateTime() + "',";
//
//        }
//        if (newsModel.getNumber()!=-1) {
//            sql += " number ='" + newsModel.getNumber() + "',";
//
//        }
//        if (!"".equals(newsModel.getSubject())) {
//            sql += " subject ='" + newsModel.getSubject() + "',";
//
//        }
//
//        if(!"".equals(newsModel.getContent())){
//            sql+=" content='"+newsModel.getContent()+"', ";
//        }
//        sql += " update_time = now() ";
//        sql += " where id=" + newsModel.getId();
//        return JDBC.update(sql);
//    }
//
//    @Override
//    public int delete(NewsModel newsModel) {
//        String sql=" delete from news where id = "+newsModel.getId();
//        return JDBC.update(sql);
//    }
//
//    @Override
//    public NewsModel findById(NewsModel newsModel) {
//        String sql = "select " +
//                "        id, " +
//                "        name," +
//                "        source," +
//                "        author," +
//                "        number," +
//                "        date_format(create_time,'%Y-%m-%d') create_time," +
//                "        subject," +
//                "        content," +
//                "        is_show," +
//                "        is_recommend," +
//                "        enable," +
//                "        newsclassification_id," +
//                "        update_time " +
//                "from news where id = "+newsModel.getId();
//        ResultSet resultSet = JDBC.query(sql);
//        NewsModel model = null;
//        try {
//            while (resultSet.next()) {
//                model = new NewsModel();
//                model.setId(resultSet.getInt("id"));
//                model.setName(resultSet.getString("name"));
//                model.setSource(resultSet.getString("source"));
//                model.setAuthor(resultSet.getString("author"));
//                model.setNumber(resultSet.getInt("number"));
//                model.setSubject(resultSet.getString("subject"));
//                model.setContent(resultSet.getString("content"));
//                model.setCreateTime(resultSet.getString("create_time"));
//                model.setContent(resultSet.getString("content"));
//                model.setNewsclassificationId(resultSet.getInt("newsclassification_id"));
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return model;
//    }
//
//    @Override
//    public List<NewsModel> findAll(NewsModel newsModel) {
//        String sql = "select   id,name,source,author,number,date_format(create_time,'%Y-%m-%d') create_time,subject,content, is_show,is_recommend, enable,update_time ,newsclassification_id ,newsclassification_name from (select p.*,c.name newsclassification_name from news p left join newsclassification c on p.newsclassification_id=c.id) temp where 1=1 ";
//
//        if (!"".equals(newsModel.getName())) {
//            sql += " and temp.name like '%" + newsModel.getName() + "%'";
//        }
//        if (!"".equals(newsModel.getSource())) {
//            sql += " and temp.source like '%" + newsModel.getSource() + "%'";
//        }
//        if (!"".equals(newsModel.getAuthor())) {
//            sql += " and temp.author like '%" + newsModel.getAuthor() + "%'";
//        }
//        if (!"".equals(newsModel.getCreateTimeStart())) {
//            sql += " and temp.create_time >='" + newsModel.getCreateTimeStart() + "' ";
//        }
//        if (!"".equals(newsModel.getCreateTimeEnd())) {
//            sql += " and temp.create_time <='" + newsModel.getCreateTimeEnd() + "' ";
//        }
//        sql += " order by id desc ";
//        sql += " limit " + (newsModel.getPage() - 1) * newsModel.getLimit() + "," + newsModel.getLimit();
//        ResultSet query = JDBC.query(sql);
//        List<NewsModel> list=new ArrayList<>();
//        try {
//            while (query.next()){
//                NewsModel model=new NewsModel();
//                model.setId(query.getInt("id"));
//                model.setName(query.getString("name"));
//                model.setSubject(query.getString("subject"));
//                model.setNumber(query.getInt("number"));
//                model.setAuthor(query.getString("author"));
//                model.setSource(query.getString("source"));
//                model.setContent(query.getString("content"));
//                model.setIsShow(query.getInt("is_show"));
//                model.setIsRecommend(query.getInt("is_recommend"));
//                model.setCreateTime(query.getString("create_time"));
//                model.setUpdateTime(query.getString("update_time"));
//                model.setEnable(query.getInt("enable"));
//                model.setNewsclassificationId(query.getInt("newsclassification_id"));
//                model.setNewsclassificationName(query.getString("newsclassification_name"));
//                list.add(model);
//
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } ;
//        return list;
//    }
//
//    @Override
//    public int count(NewsModel newsModel) {
//        String sql = " select count(*) count from news where 1=1";
//        ResultSet query = JDBC.query(sql);
//        try {
//            while (query.next()) {
//                return query.getInt("count");
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return 0;
//    }
//}
